import { AppType } from "@/Util/Constant"
import * as PIXI from 'pixi.js'
import Hlddz from "./model/hlddz.ts";
import { Util, getUserInfo } from "@/Util/index.ts";
import { GameAudio } from "./audio/audio.ts";
import { PixiBase } from "./baseClass/PixiBase.ts";
import { Broadcaster, EventType } from "./broadcaster/Broadcaster.ts";

let resourceMap: Resource[] = [
    { name: 'game-bg', path: '/images/game-bg.png' },
    { name: 'gril-l', path: '/images/girl-left.png' },
    { name: 'gril-r', path: '/images/girl-right.png' },
    { name: 'land', path: '/images/land.png' },
    { name: 'skip', path: '/images/skip.png' },
    { name: 'timer', path: '/images/timer.png' },
    { name: 'hld', path: '/images/hld.png' },
    { name: 'double', path: '/images/double.png' },
    { name: 'land-icon', path: '/images/land-icon.png' },
    { name: 'win', path: '/images/win.png' },
    { name: 'lose', path: '/images/lose.png' },
    { name: 'ok', path: '/images/ok.png' },
    { name: 'robot', path: '/images/robot.png' },
    { name: 'avatar', path: Util.getImgUrl(getUserInfo().avatar) },
]

interface Resource {
    name: string;
    path: string;
}

export class LoadResource extends PixiBase {
    model: Hlddz = Hlddz.getInstance()
    outer: PIXI.Graphics;
    inner: PIXI.Graphics;
    loadText: PIXI.Text;
    constructor(public app: AppType, public audio: GameAudio) {
        super(app)
        this.loaderRender()
        this.load()
    }
    loaderRender() {
        let loadBackground = PIXI.Texture.from('/images/load.png')
        let bgSprite = new PIXI.Sprite(loadBackground)
        bgSprite.width = this.app.screen.width
        bgSprite.height = this.app.screen.height
        this.baseContainer.addChild(bgSprite)
        this.renderloader()
    }
    renderloader() {
        let loaderWidth = 1200 * this.scaleSize
        let loaderHeight = 30 * this.scaleSize
        // 渲染进度条外包裹
        let outer = new PIXI.Graphics()
        outer.lineStyle(2, '#fff')
        outer.beginFill('#092d61');
        outer.drawRoundedRect(0, 0, loaderWidth, loaderHeight, 20 * this.scaleSize);
        outer.position.set((window.innerWidth - loaderWidth) / 2, window.innerHeight - 100 * this.scaleSize);
        outer.endFill();
        this.outer = outer
        // 渲染进度条内包裹
        let inner = new PIXI.Graphics()
        inner.beginFill('#0fb5b5');
        inner.drawRoundedRect(0, 0, loaderWidth, loaderHeight - 6 * this.scaleSize, 30 * this.scaleSize);
        inner.position.set(5 * this.scaleSize, 3 * this.scaleSize);
        inner.endFill();
        this.inner = inner
        // 渲染文字
        this.loadText = new PIXI.Text('', {
            fontFamily: "微软雅黑",
            fill: '#fff',
            fontSize: 20 * this.scaleSize,
            align: "center",
        }) as PIXI.Text;
        // 添加进容器
        outer.addChild(inner)
        outer.addChild(this.loadText)
        this.loadText.position.set((this.outer.width - this.loadText.width) / 2, 0)
        this.baseContainer.addChild(outer)
    }
    setLoadText(text: string) {
        this.loadText.text = text
        this.loadText.position.set((this.outer.width - this.loadText.width) / 2, 0)
    }
    async load() {
        let loaderWidth = 1200 * this.scaleSize
        let resName = []
        resourceMap.forEach(({ name, path }) => {
            let cache = PIXI.utils.TextureCache[name]
            if (!cache) {
                PIXI.Assets.add(name, path)
                resName.push(name)
            }
        })
        if (resName.length == 0) {
            this.inner.width = loaderWidth
        } else {
            await PIXI.Assets.load(resName, (process) => {
                this.setLoadText('正在加载图像资源...  ' + (process * 100).toFixed(0) + '%')
                this.inner.width = loaderWidth * process
            })
        }
        this.inner.width = 1
        this.setLoadText('正在加载音频资源...  0%')
        await this.audio.loadBgmAudio((process: number) => {
            this.setLoadText('正在加载音频资源...  ' + (process * 100).toFixed(0) + '%')
            this.inner.width = loaderWidth * process
        })
        this.app.stage.removeChild(this.baseContainer)
        this.model.finishLoad()

    }
    notify(caster: Broadcaster, event: EventType): void {
    }
}